<?php
require $root . '/ArrayStruct/ArrayStruct.php';
require 'Stack.php';

/**
 * 数组实现栈
 * Class StackStruct
 */
class StackStruct implements Stack
{
    //数组类对象，用于存放栈元素
    protected $array = null;

    /**
     * 构造函数 定义栈的容量
     * ArrayStruct constructor.
     * @param int $capacity
     */
    public function __construct(int $capacity = 10)
    {
        $this->array = new ArrayStruct($capacity);
    }

    /**
     * 获取栈大小
     * @return int
     */
    public function getSize(): int
    {
        return $this->array->getSize();
    }

    /**
     * 判断栈是否为空
     * @return bool
     */
    public function isEmpty(): bool
    {
        return $this->array->isEmpty();
    }

    /**
     * 元素入栈
     */
    public function push($e): void
    {
        $this->array->addLast($e);
    }

    /**
     * 出栈
     * @return mixed
     */
    public function pop()
    {
        return $this->array->removeLast();
    }

    /**
     * 查看栈顶元素
     * @return mixed
     */
    public function peek()
    {
        return $this->array->getLast();
    }

    /**
     * 将栈数组转化为字符串
     * @return string
     */
    public function toString(): string
    {
        return $this->array->toString();
    }
}